package com.qf.leadnewswemedia.aop;

import com.qf.leadnewsmodel.dtos.ResponseResult;
import com.qf.leadnewsmodel.enums.AppHttpCodeEnum;
import com.qf.leadnewsutils.commons.ThreadLocalUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
@Aspect //切点+通知
public class LoginVerifyAop {

    private List<String> ignoreResources = Arrays.asList("login","getById","updateById","publishNews","autoScan");

    @Pointcut("execution(* com.qf.leadnewswemedia.service..*.*(..))")
    public void pt(){

    }

    @Around("pt()")
    public Object loginVerfy(ProceedingJoinPoint pjp) throws Throwable {

        String name = pjp.getSignature().getName();
        if (!ignoreResources.contains(name)) {
            //进行登录验证
            Long uid = ThreadLocalUtil.getUid();
            if (uid == null || uid == 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }
        }
        //调用目标方法
        return pjp.proceed();
    }
}
